package org.tale.mscp.sms.aliyun;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tale.mscp.sms.aliyun.entity.SmsQueryEntity;
import org.tale.mscp.sms.aliyun.entity.SmsSendBatchEntity;
import org.tale.mscp.sms.aliyun.entity.SmsSendEntity;
import org.tale.mscp.sms.aliyun.services.SmsService;

@RestController
@RequestMapping(value = {"/alisms"})
public class SmsController {
    @Autowired
    public SmsService smsService;

    private static String SECRET = "fQH3u2qZ99f3hebTN4OvUvWrjOqlxH";
    private static String ACCESS_KEY_ID = "LTAI6kAAfusy3FIT";
    private static String SIGN_NAME = "漳城集";


    private String DoSend(SmsSendEntity smsSendEntity) {
        String response;
        try {
            response = smsService.SendSms(smsSendEntity);
        } catch (ClientException e) {
            smsSendEntity.setMessage(e.getMessage());
            smsSendEntity.setCode("CLIENT_ERROR");
            response = GsonUtils.toJson(smsSendEntity);
        }
//        System.out.println(response);
        return response;
    }

    private String DoQuery(SmsQueryEntity smsQueryEntity) {
        String response;
        try {
            response = smsService.QuerySendDetails(smsQueryEntity);
        } catch (ClientException e) {
            smsQueryEntity.setMessage(e.getMessage());
            smsQueryEntity.setCode("CLIENT_ERROR");
            response = GsonUtils.toJson(smsQueryEntity);
        }
        System.out.println(response);
        return response;
    }

    private String DoSendBatch(SmsSendBatchEntity smsSendBatchEntity) {
        String response;
        try {
            response = smsService.SendBatchSms(smsSendBatchEntity);
        } catch (ClientException e) {
            smsSendBatchEntity.setMessage(e.getMessage());
            smsSendBatchEntity.setCode("CLIENT_ERROR");
            response = GsonUtils.toJson(smsSendBatchEntity);
        }
        System.out.println(response);
        return response;
    }

    /**
     * 通用发单一短信
     *
     * @param smsSendEntity
     * @return
     */
    @RequestMapping(value = {"/sendsms"})
    public String SendSms(SmsSendEntity smsSendEntity) {
        return DoSend(smsSendEntity);
    }

    /**
     * 发送单人考勤通知
     *
     * @param phoneNumbers
     * @param name
     * @param date
     * @return
     */
    @RequestMapping(value = {"/sendkqnotify"})
    public String SendKqNotifySingle(String phoneNumbers, String name, String date, String outId) {
        SmsSendEntity smsSendEntity = new SmsSendEntity();
        smsSendEntity.setAccessKeyId(ACCESS_KEY_ID);
        smsSendEntity.setSecret(SECRET);
        smsSendEntity.setPhoneNumbers(phoneNumbers);
        smsSendEntity.setSignName(SIGN_NAME);
        smsSendEntity.setTemplateCode("SMS_156276938");
        smsSendEntity.setTemplateParam("{'name':'" + name + "','date':'" + date + "'}");
        if (!StringUtils.isEmpty(outId)) {
            smsSendEntity.setOutId(outId);
        }
        return DoSend(smsSendEntity);
    }

    /**
     * @param phoneNumber
     * @param sendDate    格式为 20190122型
     * @return
     */
    @RequestMapping(value = {"/querykqnotify"})
    public String QueryKqNotify(String phoneNumber, String sendDate) {
        SmsQueryEntity smsQueryEntity = new SmsQueryEntity();
        smsQueryEntity.setAccessKeyId(ACCESS_KEY_ID);
        smsQueryEntity.setSecret(SECRET);
        smsQueryEntity.setPhoneNumber(phoneNumber);
        smsQueryEntity.setSendDate(sendDate);
        smsQueryEntity.setPageSize((long) 50);
        smsQueryEntity.setCurrentPage((long) 1);
        return DoQuery(smsQueryEntity);
    }
}
